package {
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	import mx.controls.Alert;
	
	import org.jingle.sketch.Wall;
	import org.jingle.sketch.component.Arc;
	import org.jingle.sketch.component.Background;
	import org.jingle.sketch.component.Composite;
	import org.jingle.sketch.component.Point;
	import org.jingle.sketch.component.Rectangle;
	import org.jingle.sketch.component.Sector;
	import org.jingle.sketch.event.MagneticEvent;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;

	[SWF(width="620", height="620", backgroundColor="#ffffff", frameRate="60")]
	public class sketch extends BasicView {
		private var plane:Plane;
		private var material:ColorMaterial;
		private var mode:int;
		
		
		
		public function sketch() {
			//init();
			init2D();
		}
		
		public function setMode(mode:int):void {
			
		}
		
		private function init2D():void {
			stage.frameRate = 55;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			addEventListener(Event.ENTER_FRAME, run);
			
			var main:Composite = new Composite(null, this);
			
			var back:Background = new Background(main, new Point(10,10), 600, 600);
			back.alpha = 0.1;
			back.backgroundColor = 0xFB8EE0;
			back.paint();
			
			var arc:Arc = new Arc(back, new Point(210, 210), 100, 360);
			arc.startAngle = 0;
			arc.thickness = 1;
			arc.paint();
			
			var sector: Sector = new Sector(back, new Point(410, 410), 100, 150, 180);
			sector.pickable = true;
			sector.addDefaultFilters();
			sector.paint();
			
			var rect:Rectangle = new Rectangle(back, new Point(100, 100), 200, 100, 10); 
			rect.fillColor = 0xAB8EE0;
			rect.addDefaultAnchors();
			rect.dragEnable(true);
			rect.pickable = true;
			rect.addDefaultFilters();
			rect.paint();
			
			var wall:Wall = new Wall(back, new Point(200, 200), 100);
			wall.paint();
			
			var wall2:Wall = new Wall(back, new Point(300, 300), 100);
			wall2.paint();
			wall2.addEventListener(MagneticEvent.ON_CLOSE, magnetic);
			function magnetic():void{
				trace("on close");
			}
		}

		private function run(evt:Event):void {
			
		}
		
		private function keyDownHandler(keyEvt:KeyboardEvent):void {
		
			var keySpeed:Number = 0.2;

		}
		
		
		private function keyUpHandler(keyEvt:KeyboardEvent):void {
		}
		
		private function init():void{
			initMaterials();
			initObjects();
			startRendering();
			
			viewport.interactive  = true;
		}
		
		
		private function initObjects():void{
			plane = new Plane(material);
			
//			for each(var child:DisplayObject3D in plane.children){
//				child.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onPlaneClick);
//			}
			
			plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, onPlaneClick);
			scene.addChild(plane);
		}
		
		private function onPlaneClick(e:InteractiveScene3DEvent):void{
			plane.pitch(20);
		}
		
		private function initMaterials():void {
			material = new ColorMaterial( 0xcc0000 );
			material.doubleSided = true;
			material.interactive = true;
		}
		
		override protected function onRenderTick(event:Event=null):void{
			plane.yaw(2);
			super.onRenderTick(event);
		}
	}
}
